# !/usr/bin/python3
# -*- coding: utf-8 -*-

import re
import execjs
import requests
import urllib.parse


class Mangabz:
    """
    日本漫画漫画章节图片下载
    """
    def __init__(self, url):
        self.url = url
        self.session = requests.Session()
        self.headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
                        "Referer": self.url,
                        "Cookie": "image_time_cookie=17115|637270678077155170|2",
                        }

    def get_chapter_argv(self):
        print(self.url)
        res = requests.get(self.url, headers=self.headers, timeout=10)
        # print(res.text)
        mangabz_cid = re.findall("MANGABZ_CID=(.*?);", res.text)[0]
        mangabz_mid = re.findall("MANGABZ_MID=(.*?);", res.text)[0]
        page_total = re.findall("MANGABZ_IMAGE_COUNT=(.*?);", res.text)[0]
        mangabz_viewsign_dt = re.findall("MANGABZ_VIEWSIGN_DT=\"(.*?)\";", res.text)[0]
        mangabz_viewsign = re.findall("MANGABZ_VIEWSIGN=\"(.*?)\";", res.text)[0]
        print(mangabz_cid, mangabz_mid, page_total, mangabz_viewsign_dt, mangabz_viewsign, sep='|')
        return (mangabz_cid, mangabz_mid, mangabz_viewsign_dt, mangabz_viewsign, page_total)

    def get_images_js(self, page, mangabz_cid, mangabz_mid, mangabz_viewsign_dt, mangabz_viewsign):
        url = self.url + "chapterimage.ashx?" + "cid=%s&page=%s&key=&_cid=%s&_mid=%s&_dt=%s&_sign=%s" % (mangabz_cid, page, mangabz_cid, mangabz_mid, urllib.parse.quote(mangabz_viewsign_dt), mangabz_viewsign)
        print(url)
        res = self.session.get(url, headers=self.headers, timeout=10)
        self.headers["Referer"] = res.url
        return res.text

    def run(self):
        mangabz_cid, mangabz_mid, mangabz_viewsign_dt, mangabz_viewsign, page_total = self.get_chapter_argv()

        for i in range(int(1)):
            i += 1
            js_str = self.get_images_js(i, mangabz_cid, mangabz_mid, mangabz_viewsign_dt, mangabz_viewsign)
            print(js_str)
            imagesList = execjs.eval(js_str)
            print(imagesList[0])


if __name__ == '__main__':
    # https://www.mangabz.com/139bz/
    mangabz = Mangabz("https://www.mangabz.com/m13866/")
    mangabz.run()
"""
result:
http://image.mangabz.com/1/249/17115/1_9913.jpg?cid=17115&key=16f4cd6d4bc919191eb29b37a2bcf872&uk=
http://image.mangabz.com/1/249/17115/2_3245.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/3_6479.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/4_5401.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/5_3441.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/6_9421.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/7_3221.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/8_3533.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/9_4780.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/10_2250.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/11_3742.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/12_6481.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/13_4772.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/14_1675.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/15_1266.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/16_9599.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/17_4840.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/18_5913.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/19_1302.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/20_6355.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/21_3462.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/22_6553.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=
http://image.mangabz.com/1/249/17115/23_5102.jpg?cid=17115&key=eb6e23a95153bbd5efe6ca81ea3016f6&uk=

关于图片下载可以看我其他的爬虫文章
"""
